package org.sqp.proxy.impl;

import okhttp3.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.sqp.conn.Browser;
import org.sqp.conn.BrowserFactory;
import org.sqp.proxy.AbstractNetProxyService;
import org.sqp.proxy.model.NetProxy;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author sunqipeng
 * @date 2019/1/3
 */
public class XiCiDaiLiHttpProxyService extends AbstractNetProxyService {
    private Browser browserInstance;
    private int reqSum = 0;
    private int page = 1;

    public XiCiDaiLiHttpProxyService() {
        browserInstance = BrowserFactory.getDefaultBrowser();
    }

    @Override
    public long getMinInterval() {
        return 3333;
    }

    @Override
    public List<NetProxy> netAcquire() {
        List<NetProxy> list = new ArrayList<>();
        if (reqSum++ >= 100) {
            browserInstance = BrowserFactory.getDefaultBrowser();
        }

        try {
            Response resp = browserInstance.request(browserInstance.getRequestBuilder().url("https://www.xicidaili.com/nt/" + page++).build());
            Document doc = Jsoup.parse(resp.body().string());
            Elements elements = doc.select("#ip_list tbody tr");
            for (Element tr:elements){
                try {
                    Elements td2 = tr.select("td:nth-child(2)"); //ip
                    if (td2 == null){
                        continue;
                    }
                    Elements td3 = tr.select("td:nth-child(3)"); //端口
                    Elements td4 = tr.select("td:nth-child(4) a"); //地址
                    Elements td6 = tr.select("td:nth-child(6)"); //类型 http https
                    Elements td9 = tr.select("td:nth-child(9)"); //存活时间
                    Elements td10 = tr.select("td:nth-child(10)"); //active time
                    String ip = td2.text();
                    String port = td3.text();
                    String loc = td4.text();
                    String type = td6.text();


                    if (type != null && type.equalsIgnoreCase("https")){
                        continue;
                    }

                    Date expire = calExpireDate(td10.text(), td9.text());

                    NetProxy proxy = new NetProxy();
                    proxy.setIp(ip);
                    proxy.setLocation(loc);
                    proxy.setPort(Integer.parseInt(port));
                    proxy.setExpireDate(expire);
                    list.add(proxy);
                }catch (Exception e){
                    //
                }

            }

        }catch (Exception e){
            // nothing
        }
        return list;
    }

    //19-01-03 12:44   23天
    private Date calExpireDate(String validDate, String days){
        try {
            Date date = new SimpleDateFormat("yy-MM-dd HH:mm").parse(validDate);
            LocalDateTime time = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
            if (days.endsWith("天")){
                days = days.replace("天","");
                time = time.plusDays(Integer.parseInt(days));
            }
            if (days.endsWith("小时")){
                days = days.replace("小时","");
                time = time.plusHours(Integer.parseInt(days));
            }
            if (days.endsWith("分钟")){
                days = days.replace("分钟","");
                time = time.plusMinutes(Integer.parseInt(days));
            }
            return Date.from(time.toInstant(ZoneOffset.ofHours(8)));
        }catch (Exception e){
           return Date.from(LocalDateTime.now().plusHours(1).toInstant(ZoneOffset.ofHours(8)));
        }
    }
}
